package edu.cmu.mism.dgjava.views;

import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.forms.widgets.FormToolkit;
import org.eclipse.ui.part.ViewPart;

import edu.cmu.mism.dgjava.data.models.option.OptionCalculationResult;
import edu.cmu.mism.dgjava.utils.SWTUtils;

/**
 * 
 * Option Data View
 * 
 * @author Christian
 * 
 */
public class OptionDataView extends ViewPart {

	public static final String ID = "DGJava.view.optionData"; //$NON-NLS-1$

	private Group _optionParamsGroup;

	private Text _optionPriceTxt;

	private Text _deltaTxt;
	private Text _gammaTxt;
	private Text _vegaTxt;
	private Text _thetaTxt;
	private Text _rhoTxt;

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * org.eclipse.ui.part.WorkbenchPart#createPartControl(org.eclipse.swt.widgets
	 * .Composite)
	 */
	@Override
	public void createPartControl(Composite parent) {
		Composite container = SWTUtils.createComposite(parent, 2, -1, 1);
		((GridLayout) container.getLayout()).marginHeight = 0;
		((GridLayout) container.getLayout()).marginWidth = 0;
		container.setBackground(container.getDisplay().getSystemColor(
				SWT.COLOR_LIST_BACKGROUND));

		Composite lContainer = SWTUtils.createComposite(container, 3, -1, 1);

		// option parameters group
		createOptionsParametersGroup(lContainer);

		// hook up modify listener
		hookupModifyListener();

	}

	/**
	 * Hook up modify listener to observe modification event and validate user's
	 * input
	 */
	private void hookupModifyListener() {

		// this._deltaTxt.addModifyListener(this._validator);
		// this._exPriceTxt.addModifyListener(this._validator);
		// this._exTimeTxt.addModifyListener(this._validator);
		// this._gammaTxt.addModifyListener(this._validator);
		// this._optionPriceTxt.addModifyListener(this._validator);
		// this._rhoTxt.addModifyListener(this._validator);
		// this._thetaTxt.addModifyListener(this._validator);
		// this._vegaTxt.addModifyListener(this._validator);
	}

	/**
	 * @param container
	 */
	private void createOptionsParametersGroup(Composite container) {
		this._optionParamsGroup = SWTUtils.createGroup(container,
				Messages.OptionDataView_group_option_parameters, 1, 3, -1,
				GridData.FILL_HORIZONTAL);
		((GridLayout) this._optionParamsGroup.getLayout()).marginWidth = 0;

		Composite innerContainer = new FormToolkit(container.getDisplay())
				.createComposite(this._optionParamsGroup, SWT.NONE);
		GridLayout gl = new GridLayout();
		gl.numColumns = 2;
		innerContainer.setLayout(gl);
		GridData gd = new GridData(SWT.FILL, SWT.FILL, true, true);
		innerContainer.setLayoutData(gd);

		// option price
		Label optionPriceLbl = SWTUtils.createLabel(innerContainer,
				Messages.OptionDataView_label_price, 1);
		GridData labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		optionPriceLbl.setLayoutData(labelGd);
		this._optionPriceTxt = SWTUtils.createTextBox(innerContainer);
		GridData hGd = new GridData(SWT.FILL, -1, true, false);
		this._optionPriceTxt.setLayoutData(hGd);
		this._optionPriceTxt.setEditable(false);

		// delta
		Label deltaLbl = SWTUtils.createLabel(innerContainer,
				Messages.OptionDataView_label_delta, 1);
		labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		deltaLbl.setLayoutData(labelGd);
		this._deltaTxt = SWTUtils.createTextBox(innerContainer);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._deltaTxt.setLayoutData(hGd);
		this._deltaTxt.setEditable(false);

		// gamma
		Label gammaLbl = SWTUtils.createLabel(innerContainer,
				Messages.OptionDataView_label_gamma, 1);
		labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		gammaLbl.setLayoutData(labelGd);
		this._gammaTxt = SWTUtils.createTextBox(innerContainer);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._gammaTxt.setLayoutData(hGd);
		this._gammaTxt.setEditable(false);

		// vega
		Label vegaLbl = SWTUtils.createLabel(innerContainer,
				Messages.OptionDataView_label_vega, 1);
		labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		vegaLbl.setLayoutData(labelGd);
		this._vegaTxt = SWTUtils.createTextBox(innerContainer);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._vegaTxt.setLayoutData(hGd);
		this._vegaTxt.setEditable(false);

		// theta
		Label thetaLbl = SWTUtils.createLabel(innerContainer,
				Messages.OptionDataView_label_theta, 1);
		labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		thetaLbl.setLayoutData(labelGd);
		this._thetaTxt = SWTUtils.createTextBox(innerContainer);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._thetaTxt.setLayoutData(hGd);
		this._thetaTxt.setEditable(false);

		// rho
		Label rhoLbl = SWTUtils.createLabel(innerContainer,
				Messages.OptionDataView_label_rho, 1);
		labelGd = new GridData(GridData.HORIZONTAL_ALIGN_END);
		rhoLbl.setLayoutData(labelGd);
		this._rhoTxt = SWTUtils.createTextBox(innerContainer);
		hGd = new GridData(SWT.FILL, -1, true, false);
		this._rhoTxt.setLayoutData(hGd);
		this._rhoTxt.setEditable(false);

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.ui.part.WorkbenchPart#setFocus()
	 */
	@Override
	public void setFocus() {
		// TODO Auto-generated method stub

	}

	/**
	 * Validate User's Input
	 */
	public void validate() {
		// TODO
	}

	public void populateResult(OptionCalculationResult result) {
		if (result != null) {
			this._deltaTxt.setText("" + result.getDelta());
			this._gammaTxt.setText("" + result.getGamma());
			this._optionPriceTxt.setText("" + result.getPrice());
			this._rhoTxt.setText("" + result.getRho());
			this._thetaTxt.setText("" + result.getTheta());
			this._vegaTxt.setText("" + result.getVega());
		}

	}
}
